<?php
namespace app\home\controller;


use app\admin\util\Response;
use app\home\model\ProgramCommentModel;
use app\home\model\ProgramLikeModel;
use app\home\model\ProgramModel;
use app\home\util\Page;
use app\home\validate\ProgramValidate;
use think\Config;
use think\Db;
use think\Request;
use HTMLPurifier_Config;
use HTMLPurifier;
class Program extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        $this->_model = new ProgramModel();
        $this->_validate = new ProgramValidate();
        $this->_like_model = new ProgramLikeModel();
        $this->_comment_model = new ProgramCommentModel();
        $this->_template_add = 'add';
        $this->_template_detail = 'detail';
        $this->_template_index = 'index';
        parent::__construct($request);
    }
    protected function _before_add()
    {
        $this->assign('action',url('insert'));
        $id = input('id',0,'intval');
        $this->assign('back_url',url('index'));
        if($id != 0){
            $where = [];
            $where['id'] = $id;
            $where['uid'] = $this->_user_id;
            $vo = $this->_model->where($where)->find();
            if($vo){
                $vo['detail'] = str_replace(array("\r\n", "\r", "\n"), "", $vo['detail']);
                $vo['detail'] = str_replace('"', '\"', $vo['detail']);
                $this->assign('vo',$vo);
                $this->assign('action',url('update'));
            }
            $this->assign('back_url',url('user/index',['type'=>4]));
        }
    }
    public function update(){
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['id'] = input('id','','intval');
        $config = HTMLPurifier_Config::createDefault();
        $purifier = new HTMLPurifier($config);
        $param['detail'] = $purifier->purify($param['detail']);
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $result = $this->_model->isUpdate(true)->save($param,['id'=>$param['id']]);
            if($result){
                Response::json(1,'更新成功');
            }else{
                Response::json(-1,'更新失败');
            }
        }
    }
    public function insert(){
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $config = HTMLPurifier_Config::createDefault();
        $purifier = new HTMLPurifier($config);
        $param['detail'] = $purifier->purify($param['detail']);
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $result = $this->_model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
    /**
     * 输出列表页
     * @return \think\response\View
     */
    public function index()
    {
        $order = input('order','1');
        $list = $this->_get_list($order);
        $this->assign('order',$order);
        foreach ($list->items() as $value){
            $value['comment_num'] = $this->_get_like_num($value['id']);
        }
        $this->assign('list',$list);
        return view($this->_template_index);
    }


}
